package com.salon.server.service;

import com.salon.server.config.MinioConfiguration;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class MinioService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConfiguration minioConfiguration;

    public String uploadImage(MultipartFile file) {
        // minio上传文件
        try {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(minioConfiguration.getBucketName())
                            .object("image/" + file.getOriginalFilename())
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build());

            return minioConfiguration.getEndpoint() + "/" + minioConfiguration.getBucketName() + "/image/" + file.getOriginalFilename();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
